package net.dntuan.simple.web.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * @author tuan.dang
 *
 * <br>$LastChangedRevision: 36 $ 
 * <br>$LastChangedDate: 2014-04-07 16:51:11 +0000 (Mon, 07 Apr 2014) $
 */
public class ServletLifeCycle extends HttpServlet {
	private static final long serialVersionUID = -1731021641295248680L;

	private static final Logger LOGGER = Logger.getLogger(ServletLifeCycle.class.getName());

	public ServletLifeCycle() {
		LOGGER.info("I am default constructor.");
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		LOGGER.info("initialize servlet. be called ONLY once");
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.getWriter().write("view log for debugging on servlet life cycle");
		LOGGER.info("do something");
		req.getSession(true);
	}

	@Override
	public void destroy() {
		super.destroy();
		LOGGER.info("destroy servlet. be called ONLY once");
	}
}
